package top.lbqaq.dao;

import top.lbqaq.pojo.Product;
import top.lbqaq.pojo.ProductIn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Gor
 */
public class ProductInDao {
    public List getAllProductInpg(int startIndex, int pagesize, Connection connection) throws SQLException {
        ProductIn product;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<ProductIn> products = new LinkedList<>();
        if (connection != null) {

            String sql = "select * from product_in limit ?,?";
            Object[] params = {startIndex,pagesize};

            rs = BaseDao.execute(connection, pstm, rs, sql,params);
            while (rs.next()) {
                product = new ProductIn();
                product.setProductCategory1(rs.getString("product_category1"));
                product.setProductCategory2(rs.getString("product_category2"));
                product.setProductInDate(rs.getDate("product_in_date"));
                product.setProductName(rs.getString("product_name"));
                product.setProductUnit(rs.getString("product_unit"));
                product.setProductSupplier(rs.getString("product_supplier"));
                product.setProductRemark(rs.getString("product_remark"));
                product.setProductNumber(rs.getInt("product_number"));
                product.setId(rs.getInt("id"));
                // System.out.println(product.getId());
                // product.setId(rs.getInt("id"));
                products.add(product);
            }
            BaseDao.closeResource(null, pstm, rs);
        }
        return products;
    }
    public List<ProductIn> getAllProductIn(Connection connection) throws SQLException {
        ProductIn product;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<ProductIn> products = new LinkedList<>();
        if (connection != null) {
            String sql = "select * from product_in";
            rs = BaseDao.execute(connection, pstm, rs, sql);
            while (rs.next()) {
                product = new ProductIn();
                product.setProductCategory1(rs.getString("product_category1"));
                product.setProductCategory2(rs.getString("product_category2"));
                product.setProductInDate(rs.getDate("product_in_date"));
                product.setProductName(rs.getString("product_name"));
                product.setProductUnit(rs.getString("product_unit"));
                product.setProductSupplier(rs.getString("product_supplier"));
                product.setProductRemark(rs.getString("product_remark"));
                product.setProductNumber(rs.getInt("product_number"));
                product.setId(rs.getInt("id"));
                // System.out.println(product.getId());
                // product.setId(rs.getInt("id"));
                products.add(product);
            }
            BaseDao.closeResource(null, pstm, rs);
        }
        return products;

    }
    public int updateInById(Connection connection,int id,int number) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        int n=0;
        if (connection != null) {
            String sql = "select product_number from product_in where product_id=?";
            Object[] params = {id};
            rs = BaseDao.execute(connection, pstm, rs, sql,params);
            int x=0;
            while (rs.next()) {x=rs.getInt("product_number");}

            String sql1 = "update product_in set product_number=? where id = ?";
            Object[] params1 = {x+number,id};
            n = BaseDao.execute(connection, pstm, sql1,params1);
            BaseDao.closeResource(null, pstm, rs);
        }
        return n;

    }
    public int insert(Connection connection,ProductIn product) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        int n=0;
        if (connection != null) {

            String sql = "insert into product_in (product_category1,product_category2,product_number,product_name,product_unit" +
                    ",product_supplier,product_remark,product_in_date) values(?,?,?,?,?,?,?,?)";
            Object[] params = {product.getProductCategory1(),product.getProductCategory2(),product.getProductNumber(),product.getProductName(),
                    product.getProductUnit(),product.getProductSupplier(),product.getProductRemark(),product.getProductInDate()};
            n = BaseDao.execute(connection, pstm, sql,params);
            BaseDao.closeResource(null, pstm, rs);
        }
        return n;

    }
}
